package com.adnaan.restaurant.controller;

import com.adnaan.restaurant.common.untils.PageResult;
import com.adnaan.restaurant.common.untils.R;
import com.adnaan.restaurant.entity.ResCollect;
import com.adnaan.restaurant.service.IResCollectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * author: adnaan
 * create: 2023-12-13 19:18
 * Description:
 */
@Tag(name = "店铺收藏接口", description = "店铺收藏功能")
@RestController
@RequestMapping("/api/collect")
public class ResCollectController extends BaseController {

    @Autowired
    private IResCollectService resCollectService;

    @Operation(summary = "收藏列表")
    @GetMapping("/list")
    public R list(Integer pageNum, Integer pageSize, ResCollect resCollect) {
        resCollect.setUserId(this.getUserId());
        PageResult list = resCollectService.selectResCollect(pageNum, pageSize, resCollect);
        return R.success(list);
    }

    @Operation(summary = "是否已收藏")
    @GetMapping("/isCollect")
    public R isCollect(ResCollect resCollect) {
        resCollect.setUserId(this.getUserId());
        List<ResCollect> list = resCollectService.selectResCollectList(resCollect);
        if (list.isEmpty()) {
            return R.success().put("isCollect", "0");
        }
        return R.success().put("isCollect", "1");
    }

    @Operation(summary = "收藏按钮")
    @PostMapping("/addCollect")
    public R addCollect(ResCollect resCollect) {
        resCollect.setUserId(this.getUserId());
        List<ResCollect> list = resCollectService.selectResCollectList(resCollect);
        if (list.isEmpty()) {
            return R.success(resCollectService.addCollect(resCollect));
        } else {
            return R.success(resCollectService.cancelCollect(resCollect));
        }
    }

    @Operation(summary = "取消收藏")
    @DeleteMapping("/delCollect")
    public R delCollect(@RequestParam Long shopId) {
        ResCollect resCollect = new ResCollect();
        resCollect.setUserId(this.getUserId());
        resCollect.setShopId(shopId);
        return R.success(resCollectService.cancelCollect(resCollect));
    }
}
